home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 10.02 - access / AccessApplet.java < prev    next >
Text File  |  1996-04-22  |  2KB  |  80 lines

  1. /* -------------------------------------------------------------
  2. This applet uses a small class hierarchy to illustrate how to
  3. define abstract classes, superclasses, subclasses, and private
  4. and protected variables.
  5.  
  6. Java's classes: Applet    (applet)
  7.                 System    (lang)
  8.                 Color     (awt)
  9.  
  10. Custom classes: AccessApplet
  11.                 Shape
  12.                 Circle
  13.                 Square
  14.  
  15. ------------------------------------------------------------- */
  16. import java.awt.Color;
  17.  
  18. public class AccessApplet extends java.applet.Applet {
  19.  
  20.    public void init() {
  21.    
  22.       Circle c = new Circle();
  23.       Square s = new Square();
  24.       
  25.       c.setColor(Color.blue);
  26.       s.setColor(Color.black);
  27.       
  28.       c.x = 50;
  29.       c.y = 60;
  30.       
  31.       s.x = 100;
  32.       s.y = 200;
  33.       
  34.       c.draw();
  35.       s.draw();
  36.    
  37.    }
  38.  
  39. }
  40.  
  41. /** Shapes provide common characteristics for the circle and square. */
  42. abstract class Shape {
  43.    static protected final int radius = 20;
  44.    
  45.    private Color color;
  46.    int   x;
  47.    int   y;
  48.    
  49.    abstract void draw();
  50.    
  51.    void setColor(Color color) {
  52.       if (color == Color.black)
  53.          this.color = Color.white;
  54.       else
  55.          this.color = color;
  56.    }
  57.    
  58.    Color getColor() {
  59.       return color;
  60.    }
  61.    
  62. }
  63.  
  64. /** Draws and maintains circle information. */
  65. class Circle extends Shape {
  66.    void draw() {
  67.       System.out.println("Circle: radius = " + radius);
  68.       System.out.println("Circle: color = " + getColor().toString());
  69.    }
  70. }
  71.  
  72. /** Draws and maintains square information. */
  73. class Square extends Shape{
  74.    void draw() {
  75.       System.out.println("Square: radius = " + radius);
  76.       System.out.println("Square: color = " + getColor().toString());
  77.    }
  78. }
  79.  
  80.